package com.five.years.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.five.years.common.SystemCode;
import com.five.years.common.exception.BusinessException;
import com.five.years.common.vo.AliBarCodeVO;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zugen.wang
 * @Description
 * @Date 2021/1/30 0030 14:14
 **/
@Slf4j
public class DrugUtil {

    private static final String appCode = SpringUtils.getProperties("alibarcode.appcode");

    public AliBarCodeVO getInfo(String code) throws Exception{
        String url = "https://ali-barcode.showapi.com/barcode";

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appCode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("code", code);
        try{
            String response = HttpUtil.get(url, querys, headers);
            JSONObject object = JSONObject.parseObject(response);
            Short resCode = object.getShort("showapi_res_code");
            AliBarCodeVO codeVO = new AliBarCodeVO();
            if( resCode == 0){
                Object resBody = object.get("showapi_res_body");
                codeVO = JSON.toJavaObject((JSON) resBody, AliBarCodeVO.class);
            }else{
                log.info("Ali BarCode search code is non-existent",code);
            }
            return codeVO;
        }catch (Exception e){
            throw new BusinessException(SystemCode.CHECK_CODE_IS_IT_CORRECT);
        }


    }


}
